########################################################################
#################        Importing packages      #######################
########################################################################
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager


# 采用SQLAlchemy这个组件进行数据库的创建和交互操作
db = SQLAlchemy()
def create_app():
    app = Flask(__name__)
    app.config['SECRET_KEY'] = 'secret-key-goes-here' # flask扩展用到的密钥
    app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:HYC20021017.2.2@localhost:3306/auth_simple' # 配置mysql数据库
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False 
    db.init_app(app) # 初始化数据库
    login_manager = LoginManager() 
    login_manager.login_view = 'auth.login'
    login_manager.init_app(app) 
    from models import User
    @login_manager.user_loader
    def load_user(user_id): 
        return User.query.get(int(user_id))
    
    # 注册蓝图，flask的蓝图可以让路由组织有更好的封装性
    from auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint)
    from main import main as main_blueprint
    app.register_blueprint(main_blueprint)
    return app